package com.kx.user.settings;

import com.kx.user.mapper.SystemSettingsMapper;
import com.kx.user.pojo.SystemSettings;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.TreeMap;

/**
 * 配置信息类，数据库表中存储的是key,value数据
 *
 * @Author Sun
 * @Date 2021/7/16 17:43
 */
@Component
public class Settings {

    @Resource
    private SystemSettingsMapper systemSettingsMapper;

    /**
     * 没有必要用线程安全的map，因为此map整个应用只读。
     */
    private TreeMap<String, String> treeMapSettings;


    @PostConstruct
    public void init() {
        //查询全部数据
        List<SystemSettings> systemSettings = systemSettingsMapper.selectByExample(null);
        treeMapSettings = new TreeMap<>();
        if (systemSettings != null && systemSettings.size() > 0) {
            //把系统配置表转化为TreeMap  方便之后直接使用
            systemSettings.forEach(e->treeMapSettings.put(e.getKey(), e.getValue()));
        }
    }

    public String  get(String key) {
        return treeMapSettings.get(key);
    }
}
